package ext.tianma.changeManager.service;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.remoting.support.RemoteAccessor;
import org.springframework.stereotype.Service;

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTCollection;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.part.WTPartSubstituteLink;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.util.WTRuntimeException;
import ext.com.core.CoreUtil;
import ext.tianma.bom.eco.SubstituteLinkUtil;
import ext.tianma.changeManager.constants.ChangeConstants;
import ext.tianma.changeManager.util.ChangeUtil;
import ext.tianma.changeManager.util.QueryUtil;

@Service
public class CheckService extends RemoteAccessor{
	/**
	 * BOM校验入口
	 * @param data
	 * @param num
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject checkEntrance(String data) {
		JSONObject result = null;
		try {
			if(StringUtils.isNotEmpty(data)){
				JSONObject dataObj = new JSONObject(data);
				String bomOption = ChangeUtil.getJSONString(dataObj, "bomOption");
				String dataarray = ChangeUtil.getJSONString(dataObj, "data");
				if(StringUtils.isNotEmpty(bomOption) && StringUtils.isNotEmpty(dataarray)) {
					JSONArray array = new JSONArray(dataarray);
					if(ChangeConstants.ADDPart.equals(bomOption)) 
						result = addPartValidate(array);
					else if(ChangeConstants.DelPart.equals(bomOption))
						result = delPartValidate(array);
					else if(ChangeConstants.ReplacePart.equals(bomOption))
						result = replacePartCheck(array);
					else if(ChangeConstants.UpdatePart.equals(bomOption))
						result = modifyPartAmountValidate(array);
					else if(ChangeConstants.ADDReplcePart.equals(bomOption))
						result = addReplacePartValidate(array);
					else if(ChangeConstants.DelReplcePart.equals(bomOption))
						result = delReplacePartValidate(array);
					else if(ChangeConstants.UpdateReplcePart.equals(bomOption))
						result = replaceOldPartValidate(array);
					else if(ChangeConstants.UpdateAPart.equals(bomOption))
						result = repalcePartMainValidate(array);
					else if(ChangeConstants.ReplaceABPart.equals(bomOption))
						result = convertPartMainCheckValidate(array);
					else if(ChangeConstants.UpdateSubstitutesQuantity.equals(bomOption))
						result = addSubstitutesQuantityCheckValidate(array);
					else 
						result = ChangeUtil.resFailed("暂无此操作方式");
				} else {
					result = ChangeUtil.resFailed("bomOption和data为空");
				}
			} else {
				result = ChangeUtil.resFailed("数据为空");
			}
		} catch (JSONException e) {
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 新增物料数据校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月3日
	 */
	public JSONObject addPartValidate(JSONArray array) {
		JSONObject obj = new JSONObject();
		JSONArray dataarray = new JSONArray();
		String result = "";
		try {
			JSONObject ob = null;
			String addNum = "";
			String effectNumber = "";
			String view = "";
			String amount = "";
			String rowNumber = "";
			for (int i = 0; i < array.length(); i++) {
				ob = (JSONObject) array.get(i);
//				rowNumber = ob.getString("rowNumber");//数据行号
				rowNumber = String.valueOf((i+1));
				addNum = ob.getString("addNum"); //新增物料号
				amount = ob.getString("addAmount");//新增料用量
				effectNumber = ob.getString("effectNumber"); //受影响对象
				view = ob.getString("view");//视图
				
				WTPart childpart = CoreUtil.getWTPartByNumberAndView(addNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				try {
					if (StringUtils.isNotEmpty(amount)) {
						Double.parseDouble(amount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "用量 " + amount + "不合法\r";
				}
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图";
				}
				if(null == childpart) {
					result += "第" + rowNumber + "行" + ChangeConstants.addpartname + addNum + "不存在" + view + "视图";
				}
				if(null != bompart && null != childpart) {
					if ("OBSOLESCENCE".equals(childpart.getState().getState().toString())) { //废弃的不能使用
						result += ChangeConstants.addpartname + addNum + "状态为废弃，不能作为主料\r";
					}
					
					//判断是否已存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					// 新增物料号是否已经存在bom中
					QueryResult qr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(childpart.getMasterReference().getObject()));//使用关系
					if(qr.size() > 0) {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.addpartname + addNum + "已存在于" + ChangeConstants.effectpartname+ effectNumber + "\r";
					} else {
						// 返回的datalist中是有效的受影响对象
						JSONObject obj2 = new JSONObject();
						obj2.put("effectNumber", effectNumber);
						obj2.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
						obj2.put("effectUnit", bompart.getDefaultUnit());
						obj2.put("view", view);
						obj2.put("unit", childpart.getDefaultUnit());
						obj2.put("state", childpart.getState().getState().getDisplay(Locale.getDefault()));
						obj2.put("rowNumber", rowNumber);
						obj2.put("addNum", addNum);
						obj2.put("addAmount", amount);
						dataarray.put(obj2);
					}
				}
			}
			//返回数据集
			obj = ChangeUtil.resSuccess(dataarray, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(result = e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(result = e.getLocalizedMessage(Locale.getDefault()));
		} catch (Exception e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(result = e.getLocalizedMessage());
		}
		return obj;
	}
	
	/**
	 * 删除物料校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月3日
	 */
	public JSONObject delPartValidate(JSONArray array) {
		JSONObject obj = new JSONObject();
		JSONArray dataarray = new JSONArray();
		String result = "";
		try {
			String delNum = "";// 删除物料
			String effectNumber = "";// 受影响BOM号
			String view = ""; //视图
			String rowNumber = ""; //数据行号
			JSONObject ob = null;
			JSONObject obj3 = null;
			for (int i = 0; i < array.length(); i++) {
				ob = (JSONObject) array.get(i);
				delNum = ob.getString("delNum");
				effectNumber = ob.getString("effectNumber");
				view = ob.getString("view");
//				rowNumber = ob.getString("rowNumber");
				rowNumber = String.valueOf((i+1));
				WTPart childpart = CoreUtil.getWTPartByNumberAndView(delNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图\r";
				}
				if(null == childpart) {
					result += "第" + rowNumber + "行" + ChangeConstants.delectpartname + delNum + "不存在" + view + "视图\r";
				}
				if(null != bompart && null != childpart) {
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					// 物料号是否已经存在bom中
					QueryResult qr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(childpart.getMasterReference().getObject()));//使用关系
					if(qr.size() <= 0) {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.delectpartname + delNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + "\r";
					} else {
						obj3 = new JSONObject();
						WTPartUsageLink link = (WTPartUsageLink) qr.nextElement(); //父料跟子料，只会有一个使用关系
						
						obj3.put("rowNumber", rowNumber);
						obj3.put("effectNumber", effectNumber);
						obj3.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
						obj3.put("view", view);
						obj3.put("delLineNumber", link.getLineNumber().getValue());//子料行号
						obj3.put("delAmount", link.getQuantity().getAmount());//子料数量
						obj3.put("delUnit", link.getQuantity().getUnit());//子料单位
						obj3.put("delState", childpart.getState().getState().getDisplay(Locale.getDefault()));//子料状态
						obj3.put("delNum", delNum);
						dataarray.put(obj3);
					}
				}
			}
			//返回数据集
			obj = ChangeUtil.resSuccess(dataarray, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return obj;
	}
	
	/**
	 * 替换物料校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月4日
	 */
	public JSONObject replacePartCheck(JSONArray array) {
		String result = "";
		JSONObject obj = new JSONObject();
		JSONArray dataarray = new JSONArray();
		try {
			String oldNum = "";// 原料号
			String newNum = ""; //新料号
			String newAmount = ""; //新用量
			String effectNumber = "";// 受影响BOM号
			String view = ""; //视图
			String rowNumber = ""; //数据行号
			JSONObject ob = null;
			for (int i = 0; i < array.length(); i++) {
				ob = (JSONObject) array.get(i);
				oldNum = ob.getString("oldNum");
				newNum = ob.getString("newNum");
				effectNumber = ob.getString("effectNumber");
				view = ob.getString("view");
//				rowNumber = ob.getString("rowNumber");
				rowNumber = String.valueOf((i+1));
				newAmount = ob.getString("newAmount");
				
				WTPart oldpart = CoreUtil.getWTPartByNumberAndView(oldNum, view);
				WTPart newpart = CoreUtil.getWTPartByNumberAndView(newNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				try {
					if (StringUtils.isNotEmpty(newAmount)) {
						Double.parseDouble(newAmount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "用量 " + newAmount + "不合法\r";
				}
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == oldpart) {
					result += "第" + rowNumber + "行"
							+ ChangeConstants.oldpartname + oldNum + "不存在" + view + "视图;\r";
				}
				if(null == newpart) {
					result += "第" + rowNumber + "行" + ChangeConstants.newpartname + newNum + "不存在" + view + "视图;\r";
				}
				
				if(null != bompart && null != oldpart && null != newpart) {
					if ("OBSOLESCENCE".equals(newpart.getState().getState().toString())) { //废弃的不能使用
						result += "第" + rowNumber + "行" + ChangeConstants.newpartname + newNum + "状态为废弃，不能作为主料";
					}
					if (effectNumber.equals(newNum)) {
						result += "第" + rowNumber + "行" + ChangeConstants.newpartname + newNum + "与受影响对象编号相同";
					}
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					
					//新料是否存在bom中
					boolean isQr = true;
					QueryResult newqr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(newpart.getMasterReference().getObject()));//使用关系
					if (newqr.size() > 0) {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.newpartname + newNum + "已存在于" + ChangeConstants.effectpartname + effectNumber + ";\r";
						isQr = false;
					}
					//旧料是否不存在BOM中
					QueryResult oldqr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(oldpart.getMasterReference().getObject()));//使用关系
					if (oldqr.size() <= 0) {
						result += "第" + rowNumber + "行"
								+ ChangeConstants.effectpartname + effectNumber + "中不存在" + ChangeConstants.oldpartname + oldNum + ";\r";
						isQr = false;
					}
					
					if(isQr) {
						WTPartUsageLink oldlink = (WTPartUsageLink) oldqr.nextElement();

						JSONObject obj22 = new JSONObject();
						obj22.put("rowNumber", rowNumber);
						obj22.put("effectNumber", effectNumber);
						obj22.put("view", view);
						obj22.put("oldNum", oldNum);
						obj22.put("oldAmount", oldlink.getQuantity().getAmount());
						obj22.put("oldLineNumber", oldlink.getLineNumber().getValue());
						obj22.put("oldState", oldpart.getState().getState().getDisplay(Locale.getDefault()));
						obj22.put("oldUnit", oldpart.getDefaultUnit());
						obj22.put("newAmount", newAmount); // 新用量返回输入的用量
						obj22.put("newLineNumber", oldlink.getLineNumber().getValue()); //新行号采用旧行号
						obj22.put("newNum", newNum);
						obj22.put("newState", newpart.getState().getState().getDisplay(Locale.getDefault()));
						obj22.put("newUnit", newpart.getDefaultUnit());
						dataarray.put(obj22);
					}
				}
			}
			//返回数据集
			obj = ChangeUtil.resSuccess(dataarray, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return obj;
	}
	
	/**
	 * 用量修改校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject modifyPartAmountValidate(JSONArray array) {
		String result = "";
		JSONObject obj = new JSONObject();
		JSONArray dataarray = new JSONArray();
		try {
			String oldNum = "";// 原料号
			String newAmount =  "";// 变更后用量
			String effectNumber = "";// 受影响对象编号
			String view = ""; //视图
			String rowNumber = ""; //数据行号
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				oldNum = job.getString("oldNum");
				newAmount = job.getString("newAmount");
				effectNumber = job.getString("effectNumber");
				view = job.getString("view");
//				rowNumber = job.getString("rowNumber"); //数据行号
				rowNumber = String.valueOf((i+1));
				try {
					if (StringUtils.isNotEmpty(newAmount)) {
						Double.parseDouble(newAmount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "用量 " + newAmount + "不合法\r";
				}
				
				WTPart oldpart = CoreUtil.getWTPartByNumberAndView(oldNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图\r";
				}
				if(null == oldpart) {
					result += "第" + rowNumber + "行" + ChangeConstants.delectpartname + oldNum + "不存在" + view + "视图\r";
				}
				if(null != bompart && null != oldpart) {
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					QueryResult oldqr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(oldpart.getMasterReference().getObject()));//使用关系
					if(oldqr.size() > 0) {
						WTPartUsageLink link2 = (WTPartUsageLink) oldqr.nextElement();
						JSONObject objlink = new JSONObject();
						objlink.put("oldNum", oldNum);
						objlink.put("oldAmount", link2.getQuantity().getAmount());
						objlink.put("oldLineNumber", link2.getLineNumber().getValue());
						objlink.put("oldState", oldpart.getState().getState().getDisplay(Locale.getDefault()));
						objlink.put("oldUnit", link2.getQuantity().getUnit());
						objlink.put("newAmount", newAmount);
						objlink.put("effectNumber", effectNumber);
						objlink.put("rowNumber", rowNumber);
						objlink.put("view", view);
						dataarray.put(objlink);
					} else {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.changepartname + oldNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + ";\r";
					}
				}
			}
			//返回数据集
			obj = ChangeUtil.resSuccess(dataarray, result, "暂无数据");
		}  catch (JSONException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return obj;
	}
	
	/**
	 * 新增替代料校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject addReplacePartValidate(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		JSONArray arraylist = new JSONArray();
		try {
			String mainNum = "";// 主料号
			String partBNum = "";// 替代料号
			String partBAmount = "";// 替代料用量
			String effectNumber = "";// 受影响对象编号
			String view = "";// view
			String rowNumber = "";
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				effectNumber = job.getString("effectNumber");
				view = job.getString("view");
//				rowNumber = job.getString("rowNumber");
				rowNumber = String.valueOf((i+1));
				
				mainNum = job.getString("mainNum");
				partBNum = job.getString("newNum");
				partBAmount = job.getString("newAmount");
				
				
				WTPart mainPart = CoreUtil.getWTPartByNumberAndView(mainNum, view);
				WTPart partB = CoreUtil.getWTPartByNumberAndView(partBNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				try {
					if (StringUtils.isNotEmpty(partBAmount)) {
						Double.parseDouble(partBAmount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "用量 " + partBAmount + "不合法\r";
				}
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == mainPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.mainpartname + mainNum + "不存在" + view + "视图\r";
				}
				if(null == partB) {
					result += "第" + rowNumber + "行" + ChangeConstants.newaddreplacepartname + partBNum + "不存在" + view + "视图;\r";
				}
				if(null != bompart && null != mainPart && null != partB) {
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					QueryResult oldqr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(mainPart.getMasterReference().getObject()));//使用关系
					if(oldqr.size() > 0) {
						//新增加的料是否与主料有替代关系》》》》满足条件：否
						WTPartUsageLink link2 = (WTPartUsageLink) oldqr.nextElement();
						WTCollection wtcol2 = WTPartHelper.service.getSubstituteLinks(link2);
						WTPartSubstituteLink sublink2 = SubstituteLinkUtil.getSubstituteLink(
								(WTPartMaster) partB.getMasterReference().getObject(), wtcol2);
						if(null != sublink2) {
							result += "第" + rowNumber + "行" + 
									ChangeConstants.newaddreplacepartname + partBNum + "与" +ChangeConstants.mainpartname + mainNum + "替换关系已存在;\r";
						} else {
							JSONObject obj3 = new JSONObject();
							// 主料号
							obj3.put("mainNum", mainNum);
							obj3.put("mainLineNumber", link2.getLineNumber().getValue());
							obj3.put("mainUnit", mainPart.getDefaultUnit());
							obj3.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
							obj3.put("newNum", partBNum);
							obj3.put("newAmount", partBAmount);
							obj3.put("newUnit", partB.getDefaultUnit());
							obj3.put("newState", partB.getState().getState().getDisplay(Locale.getDefault()));
							obj3.put("effectNumber", effectNumber);
							obj3.put("view", view);
							obj3.put("rowNumber", rowNumber);
							arraylist.put(obj3);
						}
					} else {
						result += "第" + rowNumber + "行" + ChangeConstants.mainpartname + mainNum + "不存在于" + ChangeConstants.effectpartname
								+ effectNumber + ";\r";
					}
				}
			}
			//返回数据集
			object = ChangeUtil.resSuccess(arraylist, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return object;
	}
	
	/**
	 * 删除替代料校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject delReplacePartValidate(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		JSONArray arraylist = new JSONArray();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				String effectNumber = job.getString("effectNumber");// 受影响对象编号
				String view = job.getString("view");// view
//				String rowNumber = job.getString("rowNumber");
				String rowNumber = String.valueOf((i+1));
				String mainNum = job.getString("mainNum");// 主料号
				String delNum = job.getString("delNum");// 替代料号
				
				WTPart partA = CoreUtil.getWTPartByNumberAndView(mainNum, view);
				WTPart partB = CoreUtil.getWTPartByNumberAndView(delNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == partA) {
					result += "第" + rowNumber + "行" + ChangeConstants.mainpartname + mainNum + "不存在" + view + "视图;\r";
				}
				if(null == partB) {
					result += "第" + rowNumber + "行" + ChangeConstants.delreplacepartname + delNum + "不存在" + view + "视图;\r";
				}
				if(null != bompart && null != partA && null != partB) {
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					
					//主料 与 受影响对象是否有使用关系
					QueryResult qr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(partA.getMasterReference().getObject()));//使用关系
					if(qr.size() > 0) {
						//主料与被删除的替代料是否存在关系>>>>满足条件：是
						WTPartUsageLink link2 = (WTPartUsageLink) qr.nextElement();
						WTCollection wtcol2 = WTPartHelper.service.getSubstituteLinks(link2);
						WTPartSubstituteLink sublink2 = SubstituteLinkUtil.getSubstituteLink(
								(WTPartMaster) partB.getMasterReference().getObject(), wtcol2);
						if(null != sublink2) {
							JSONObject obj2 = new JSONObject();
							obj2.put("rowNumber", rowNumber);
							obj2.put("effectNumber", effectNumber);
							obj2.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("delNum", delNum);
							obj2.put("delUnit", partB.getDefaultUnit());
							obj2.put("delState", partB.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("mainUnit", partA.getDefaultUnit());
							obj2.put("mainState", partA.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("mainNum", mainNum);
							obj2.put("mainLineNumber", link2.getLineNumber().getValue());
							obj2.put("view", view);
							arraylist.put(obj2);
						} else {
							result += "第" + rowNumber + "行" + 
									ChangeConstants.delreplacepartname + delNum + "与" + ChangeConstants.mainpartname + mainNum + "不存在替换关系;\r";
						}
					} else {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.mainpartname + mainNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + ";\r";
					}
				}
			}
			//返回数据集
			object = ChangeUtil.resSuccess(arraylist, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} 
		return object;
	}
	
	/**
	 * 更换替代料校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject replaceOldPartValidate(JSONArray array) {
		JSONObject object = new JSONObject();
		JSONArray arraylist = new JSONArray();
		String result = "";
		for (int i = 0; i < array.length(); i++) {
			try {
				JSONObject job = (JSONObject) array.get(i);
				String effectNumber = job.getString("effectNumber");// 受影响对象编号
				String view = job.getString("view");// 视图
//				String rowNumber = job.getString("rowNumber");
				String rowNumber = String.valueOf((i+1));
				
				String mainNum = job.getString("mainNum");// 主料料号
				String oldNum = job.getString("oldNum");// 原替换料料号
				String newNum = job.getString("newNum");// 新替换料料号
				String newAmount = job.getString("newAmount");// 新替换料用量
				
				WTPart mainPart = CoreUtil.getWTPartByNumberAndView(mainNum, view);
				WTPart oldPart = CoreUtil.getWTPartByNumberAndView(oldNum, view);
				WTPart newPart = CoreUtil.getWTPartByNumberAndView(newNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				try {
					if (StringUtils.isNotEmpty(newAmount)) {
						Double.parseDouble(newAmount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "用量 " + newAmount + "不合法\r";
				}
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == mainPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.mainpartname + mainNum + "不存在" + view + "视图;\r";
				}
				if(null == oldPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.oldreplacepartname + oldNum + "不存在" + view + "视图;\r";
				}
				if(null == newPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.newreplacepartname + newNum + "不存在" + view + "视图;\r";
				}
				if(null != bompart && null != mainPart && null != oldPart && null != newPart) {
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					
					//主料 与 受影响对象是否有使用关系
					QueryResult qr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(mainPart.getMasterReference().getObject()));//使用关系
					if(qr.size() > 0) {
						WTPartUsageLink link2 = (WTPartUsageLink) qr.nextElement();
						WTCollection wtcol2 = WTPartHelper.service.getSubstituteLinks(link2);
						//原替代料是否与主料有替代关系》》》》满足条件：是
						WTPartSubstituteLink sublink1 = SubstituteLinkUtil.getSubstituteLink(
								(WTPartMaster) oldPart.getMasterReference().getObject(), wtcol2);
						
						//新替代料是否与主料已有替代关系》》》》满足条件：否
						WTPartSubstituteLink sublink2 = SubstituteLinkUtil.getSubstituteLink(
								(WTPartMaster) newPart.getMasterReference().getObject(), wtcol2);
						
						boolean isLink = true;
						if(null == sublink1) {
							result += "第" + rowNumber + "行" + 
									ChangeConstants.oldreplacepartname + oldNum +"与" +ChangeConstants.mainpartname + mainNum + "替换关系不存在;\r";
							isLink = false;
						}
						if(null != sublink2) {
							result += "第" + rowNumber + "行" + 
									ChangeConstants.newreplacepartname + newNum +"与" +ChangeConstants.mainpartname + mainNum + "已存在替换关系;\r";
							isLink = false;
						}
						if(isLink) {
							JSONObject obj2 = new JSONObject();
							obj2.put("effectNumber", effectNumber);
							obj2.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("effectUnit", bompart.getDefaultUnit());
							obj2.put("view", view);
							obj2.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("mainNum", mainNum);
							obj2.put("mainLineNumber", link2.getLineNumber().getValue());
							obj2.put("oldNum", oldNum);
							obj2.put("oldAmount", link2.getQuantity().getAmount());
							obj2.put("oldUnit", oldPart.getDefaultUnit());
							obj2.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("newNum", newNum);
							obj2.put("newAmount", newAmount);
							obj2.put("newState", newPart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("newUnit", newPart.getDefaultUnit());
							obj2.put("rowNumber", rowNumber);
							arraylist.put(obj2);
						}
					} else {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.mainpartname + mainNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + "中;\r";
					}
				}
				//返回数据集
				object = ChangeUtil.resSuccess(arraylist, result, "暂无数据");
			} catch (JSONException e) {
				e.printStackTrace();
				object = ChangeUtil.resFailed(e.getLocalizedMessage());
			} catch (WTRuntimeException e) {
				e.printStackTrace();
				object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
			} catch (WTException e) {
				e.printStackTrace();
				object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
			}
		}
		return object;
	}
	
	/**
	 * 更换主料校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject repalcePartMainValidate(JSONArray array) {
		JSONObject object = new JSONObject();
		JSONArray arrays = new JSONArray();
		String result = "";
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				String effectNumber = job.getString("effectNumber");// 受影响对象编号
				String view = job.getString("view");	// 视图
//				String rowNumber = job.getString("rowNumber");
				String rowNumber = String.valueOf((i+1));
				
				String oldNum = job.getString("oldNum");// 原主料料号
				String newNum = job.getString("newNum");// 新主料料料号
				String newAmount = job.getString("newAmount");// 新主料料用量
			
				WTPart oldPart = CoreUtil.getWTPartByNumberAndView(oldNum, view);
				WTPart newPart = CoreUtil.getWTPartByNumberAndView(newNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				try {
					if (StringUtils.isNotEmpty(newAmount)) {
						Double.parseDouble(newAmount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "用量 " + newAmount + "不合法\r";
				}
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == oldPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.oldmainpartname + oldNum + "不存在" + view + "视图;\r";
				}
				if(null == newPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.newmainpartname + newNum + "不存在" + view + "视图;\r";
				}
				if(null != bompart && null != oldPart && null != newPart) {
					if ("OBSOLESCENCE".equals(newPart.getState().getState().toString())) { //废弃的不能使用
						result += "第" + rowNumber + "行" + ChangeConstants.newmainpartname + newNum + "状态为废弃，不能作为主料";
					}
					if (newNum.equals(effectNumber)) {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.newmainpartname + newNum + "与受影响对象编号" + effectNumber + "相同";
					}
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					
					//旧料是否存在受影响对象中
					QueryResult oldqr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(oldPart.getMasterReference().getObject()));//使用关系
					
					//新料是否存在受影响对象中
					QueryResult newqr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(newPart.getMasterReference().getObject()));//使用关系
					
					boolean isQr = true;
					if(oldqr.size() <= 0) {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.oldmainpartname + oldNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + "中;\r";
						isQr = false;
					}
					
					if(newqr.size() > 0) {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.newmainpartname + newNum + "存在于" + ChangeConstants.effectpartname + effectNumber + "中;\r";
						isQr = false;
					}
					
					if(isQr) {
						WTPartUsageLink link2 = (WTPartUsageLink) oldqr.nextElement();
						JSONObject obj2 = new JSONObject();
						obj2.put("effectNumber", effectNumber);
						obj2.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
						obj2.put("view", view);
						obj2.put("oldNum", oldNum);
						obj2.put("oldAmount", link2.getQuantity().getAmount());
						obj2.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
						obj2.put("oldUnit", oldPart.getDefaultUnit());
						obj2.put("oldLineNumber", link2.getLineNumber().getValue());
						obj2.put("newNum", newNum);
						obj2.put("newAmount", newAmount);
						obj2.put("newState", newPart.getState().getState().getDisplay(Locale.getDefault()));
						obj2.put("newUnit", newPart.getDefaultUnit());
						obj2.put("rowNumber", rowNumber);
						arrays.put(obj2);
					}
				}
			}
			//返回数据集
			object = ChangeUtil.resSuccess(arrays, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return object;
	}
	
	/**
	 * 主次替换校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject convertPartMainCheckValidate(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		JSONArray arrays = new JSONArray();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				String mainNum = job.getString("mainNum");// 原主料料号
				String oldNum = job.getString("oldNum");// 原替代料号
				String effectNumber = job.getString("effectNumber");// 受影响对象编号
				String view = job.getString("view");// 视图
//				String rowNumber = job.getString("rowNumber");
				String rowNumber = String.valueOf((i+1));
				
				WTPart mainPart = CoreUtil.getWTPartByNumberAndView(mainNum, view);
				WTPart newPart = CoreUtil.getWTPartByNumberAndView(oldNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == mainPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.oldmainpartname + mainNum + "不存在" + view + "视图;\r";
				}
				if(null == newPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.oldreplacepartname + oldNum + "不存在" + view + "视图;\r";
				}
				if(null != bompart && null != mainPart && null != newPart) {
					if ("OBSOLESCENCE".equals(newPart.getState().getState().toString())) { //废弃的不能使用
						result += "第" + rowNumber + "行" + ChangeConstants.oldreplacepartname + oldNum + "状态为废弃，不能作为主料";
					}
					
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					//主料是否与受影响对象有关系
					QueryResult qr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(mainPart.getMasterReference().getObject()));//使用关系
					if(qr.size() > 0) {
						//主料与替代料是否存在关系>>>>满足条件：是
						WTPartUsageLink link2 = (WTPartUsageLink) qr.nextElement();
						WTCollection wtcol2 = WTPartHelper.service.getSubstituteLinks(link2);
						WTPartSubstituteLink sublink2 = SubstituteLinkUtil.getSubstituteLink(
								(WTPartMaster) newPart.getMasterReference().getObject(), wtcol2);
						if(null != sublink2) {
							JSONObject obj2 = new JSONObject();
							obj2.put("effectNumber", effectNumber);
							obj2.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("view", view);
							
							obj2.put("mainNum", mainNum);
							obj2.put("mainUnit", mainPart.getDefaultUnit());
							obj2.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("mainLineNumber", link2.getLineNumber().getValue());
							
							obj2.put("oldNum", oldNum);
							obj2.put("oldUnit", newPart.getDefaultUnit());
							obj2.put("oldState", newPart.getState().getState().getDisplay(Locale.getDefault()));
							
							obj2.put("rowNumber", rowNumber);
							arrays.put(obj2);
						} else {
							result += "第" + rowNumber + "行" + 
									ChangeConstants.oldreplacepartname + oldNum + "与" + ChangeConstants.mainpartname + mainNum +  "替换关系不存在;\r";
						}
					} else {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.oldmainpartname + mainNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + "中;\r";
					}
				}
			}
			//返回数据集
			object = ChangeUtil.resSuccess(arrays, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return object;
	}
	
	/**
	 * 替代料用量修改校验
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月20日
	 */
	public JSONObject addSubstitutesQuantityCheckValidate(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		JSONArray arrays = new JSONArray();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				String mainNum = job.getString("mainNum");// 主料料号
				String oldNum = job.getString("oldNum");// 替代料号
				String effectNumber = job.getString("effectNumber");// 受影响对象编号
				String view = job.getString("view");// 视图
				String newAmount = job.getString("newAmount");
				String rowNumber = String.valueOf((i+1));
				
				WTPart mainPart = CoreUtil.getWTPartByNumberAndView(mainNum, view);
				WTPart oldPart = CoreUtil.getWTPartByNumberAndView(oldNum, view);
				WTPart bompart = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
				
				try {
					if (StringUtils.isNotEmpty(newAmount)) {
						Double.parseDouble(newAmount);
					}
				} catch (Exception e) {
					result += "第" + rowNumber + "行" + "替代料用量 " + newAmount + "不合法\r";
				}
				
				if(null == bompart) {
					result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "不存在" + view + "视图;\r";
				}
				if(null == mainPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.oldmainpartname + mainNum + "不存在" + view + "视图;\r";
				}
				if(null == oldPart) {
					result += "第" + rowNumber + "行" + ChangeConstants.oldreplacepartname + oldNum + "不存在" + view + "视图;\r";
				}
				if(null != bompart && null != mainPart && null != oldPart) {
					// 在ECO表单中判断BOM是否存在于未完成的ECO变更流程中
					boolean flag = QueryUtil.isProcessind(bompart);
					if (flag) {
						result += "第" + rowNumber + "行" + ChangeConstants.effectpartname + effectNumber + "已存在于未关闭的ECO变更流程中\r";
					}
					//主料是否与受影响对象有关系
					QueryResult qr = PersistenceHelper.manager.find(
							WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(mainPart.getMasterReference().getObject()));//使用关系
					if(qr.size() > 0) {
						//主料与替代料是否存在关系>>>>满足条件：是
						WTPartUsageLink link2 = (WTPartUsageLink) qr.nextElement();
						WTCollection wtcol2 = WTPartHelper.service.getSubstituteLinks(link2);
						WTPartSubstituteLink sublink2 = SubstituteLinkUtil.getSubstituteLink(
								(WTPartMaster) oldPart.getMasterReference().getObject(), wtcol2);
						if(null != sublink2) {
							JSONObject obj2 = new JSONObject();
							obj2.put("effectNumber", effectNumber);
							obj2.put("effectState", bompart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("view", view);
							
							obj2.put("mainNum", mainNum);
							
							obj2.put("oldNum", oldNum);
							obj2.put("unit", oldPart.getDefaultUnit());
							obj2.put("oldAmount", sublink2.getQuantity().getAmount());
							obj2.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
							obj2.put("newAmount", newAmount);
							obj2.put("mainLineNumber", link2.getLineNumber().getValue());
							obj2.put("rowNumber", rowNumber);
							arrays.put(obj2);
						} else {
							result += "第" + rowNumber + "行" + 
									ChangeConstants.oldreplacepartname + oldNum + "与" + ChangeConstants.mainpartname + mainNum +  "替换关系不存在;\r";
						}
					} else {
						result += "第" + rowNumber + "行" + 
								ChangeConstants.oldmainpartname + mainNum + "不存在于" + ChangeConstants.effectpartname + effectNumber + "中;\r";
					}
				}
			}
			//返回数据集
			object = ChangeUtil.resSuccess(arrays, result, "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTRuntimeException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (WTException e) {
			e.printStackTrace();
			object = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return object;
	}

}
 